﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Objects;
using Northwind.NET.Settings;
using Northwind.NET.Model;
using Northwind.NET.BLL;

namespace Northwind.NET.EDM.FE.WinForms.Controls
{
    public partial class QuarterlyOrdersUserControl : BaseUserControl
    {
        public QuarterlyOrdersUserControl()
        {
            InitializeComponent();
        }

        public override string FormCaption { get { return Settings.Resources.QuarterlyOrdersFormCaption; } }
        public override dynamic GetEntitySet(ObjectContext context)
        {
            return context.CreateObjectSet<Model.Customer>();
        }

        public override BindingSource UserControlBindingSource { get { return customerBindingSource; } }

        internal override void SetBindingSource()
        {
            _context = Globals.CreateObjectContext() as NorthwindNETEntities;
            var dataSet1 = _context.CreateObjectSet<Model.Customer>();
            UserControlBindingSource.DataSource = dataSet1;

            var row = dataSet1.FirstOrDefault();
            int? customerId = null;
            if (row != null) customerId = row.ID;
            
            var dataSet2 = CustomersStatistics.QuarterlyOrders(customerId);
            orderProductQuarterDataGridView.DataSource = dataSet2;
        }

        private void customerBindingSource_CurrentChanged(object sender, EventArgs e)
        {
            int customerId = -1;
            bool test = Int32.TryParse(iDTextBox.Text, out customerId);
            if (!test)
            {
                var dataSet2 = CustomersStatistics.QuarterlyOrders(null);
                orderProductQuarterDataGridView.DataSource = dataSet2;
            }
            else
            {
                var dataSet2 = CustomersStatistics.QuarterlyOrders(customerId);
                orderProductQuarterDataGridView.DataSource = dataSet2;
            }

        }

    }
}
